from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from .models import *
# Create your views here.
import json

class Cates(View):
    def get(self,req):
        size=req.GET.get('size')
        page=req.GET.get('page')
        seh=req.GET.get('input')
        if not seh:
            data=Cate.objects.all().values()[(int(page)-1)*int(size):((int(page)-1)*int(size))+int(size)]
            c = Cate.objects.all().values().count()
            return JsonResponse({'message':'获取成功','code':200,'data':list(data),'count':c})
        if seh:
            data = Cate.objects.filter(cateName__contains=seh).values()[(int(page) - 1) * int(size):((int(page) - 1) * int(size)) + int(size)]
            c = Cate.objects.filter(cateName__contains=seh).values().count()
            return JsonResponse({'message': '获取成功', 'code': 200, 'data': list(data), 'count': c})
    def post(self,req):
        a=json.loads(req.body)
        d=Cate.objects.create(**a)
        return JsonResponse({'message': '添加成功', 'code': 200})
    def put(self,req):
        a = json.loads(req.body)
        if len(a)>2:
            lis=Cate.objects.filter(id=a['id']).update(cateName=a['cateName'],content=a['content'])
            return JsonResponse({'message': '修改成功', 'code': 200})
        if 0<=len(a)<=2:
            lis = Cate.objects.filter(id=a['id']).update(status=a['s'])
            return JsonResponse({'message':'状态成功','code':200})
        else:
            return  JsonResponse({'message':'修改失败','code':201})
    def delete(self,req,id):
        Cate.objects.filter(id=id).delete()
        return JsonResponse({'message': '删除成功', 'code': 200})

class Questions(View):
    def get(self,req):
        # print(id)
        page=req.GET.get('page')
        size=req.GET.get('size')
        input=req.GET.get('input')
        sta1 = req.GET.get('status')
        wei=req.GET.get('wei')
        a = int(page)
        b = int(size)
        cate_obj=Question.objects.all()
        o=Question.objects.all().values()[(a-1)*b:((a-1)*b)+b]
        count=Question.objects.all().values().count()
        da=Cate.objects.all().values()
        # print(o)
        l = []
        for i in cate_obj:
            # print(1111)
            d = {}
            d['id']=i.cate.id
            d['cateName']=i.cate.cateName
            d['content']=i.cate.content
            l.append(d)
            # print(l)
        for i in range(len(o)):
            o[i]['cata_o']=l[i]
        if input:
            o = Question.objects.filter(title__contains=input).values()[(a - 1) * b:((a - 1) * b) + b]
            count = Question.objects.filter(title__contains=input).values().count()
            l = []
            for i in cate_obj:
                d = {}
                d['id'] = i.cate.id
                d['cateName'] = i.cate.cateName
                d['content'] = i.cate.content
                l.append(d)
                # print(l)
            for i in range(len(o)):
                o[i]['cata_o'] = l[i]
            return JsonResponse({'code':200,'data':list(o),'data1':list(da),'count':count})
        if wei:
            o = Question.objects.filter(status=0).values()[(a - 1) * b:((a - 1) * b) + b]
            count = Question.objects.filter(status=0).values().count()
            l = []
            for i in cate_obj:
                d = {}
                d['id'] = i.cate.id
                d['cateName'] = i.cate.cateName
                d['content'] = i.cate.content
                l.append(d)
                # print(l)
            for i in range(len(o)):
                o[i]['cata_o'] = l[i]
            return JsonResponse({'code':200,'data':list(o),'data1':list(da),'count':count})
        if sta1:
            if sta1=='3':
                o = Question.objects.all().values()[(a - 1) * b:((a - 1) * b) + b]
                count = Question.objects.all().values().count()
                l = []
                for i in cate_obj:
                    d = {}
                    d['id'] = i.cate.id
                    d['cateName'] = i.cate.cateName
                    d['content'] = i.cate.content
                    l.append(d)
                    # print(l)
                for i in range(len(o)):
                    o[i]['cata_o'] = l[i]
                return JsonResponse({'code': 200, 'data': list(o), 'count': count})
            o = Question.objects.filter(status__contains=sta1).values()[(a - 1) * b:((a - 1) * b) + b]
            count = Question.objects.filter(status__contains=sta1).values().count()
            l = []
            for i in cate_obj:
                d = {}
                d['id'] = i.cate.id
                d['cateName'] = i.cate.cateName
                d['content'] = i.cate.content
                l.append(d)
                # print(l)
            for i in range(len(o)):
                o[i]['cata_o'] = l[i]
            return JsonResponse({'code': 200, 'data': list(o), 'data1': list(da), 'count': count})
        return JsonResponse({'code': 200, 'data': list(o), 'data1': list(da), 'count': count})
    #是否通过
    def put(self,req,id):
        #通过
        a =req.body
        # print(id,'同')
        Question.objects.filter(id=id).update(status=1)

        #驳回
        if a:
            b=json.loads(a)
            Question.objects.filter(id=id).update(status=2,reason=b['reason'])

            return JsonResponse({'code': 200,'message':'修改成功'})

        return JsonResponse({'code': 200, 'message': '修改成功'})
    def post(self,req):

        a=json.loads(req.body)
        Question.objects.create(title=a['title'],answer=a['answer'],uploder=a['uploader'],cate_id=a['cate'])
        return JsonResponse({'code': 200,'message':'添加成功'})


class Fquestions(View):
    def get(self,req,id):

        cate_obj=Cate.objects.get(id=id)
        a=cate_obj.question_set.all()
        L=[]
        # LL=[]
        dd = {}
        # cc={}
        for i in a:
            d={}
            d['title']=i.title
            d['answer'] = i.answer
            d['status'] = i.status
            d['uploder'] = i.uploder
            L.append(d)
        dd['cateName'] = cate_obj.cateName
        dd['content'] = cate_obj.content
        dd['moren'] = cate_obj.moren
        dd['status'] = cate_obj.status
        dd['createtime'] = cate_obj.createtime
        # dd['question_o'] = L
        for i in range(len(L)):
            L[i]['cata_o']=dd
        # print(L)

        # print(L)

        return JsonResponse({'code': 200,'data':L})

    def put(self,req):
        a=json.loads(req.body)
        Question.objects.filter(id=a['id']).update(title=a['title'],answer=a['answer'],cate_id=a['cate_id'])
        return JsonResponse({'message':'修改成功','code':200})
    def delete(self,req,id):
        Question.objects.filter(id=id).delete()
        return JsonResponse({'message': '删除成功', 'code': 200})


class Lei(View):
    def get(self,req):
        a=Cate.objects.filter().all().values()
        # print(a)
        return JsonResponse({'code': 200,'data':list(a)})
class Xq(View):
    def get(self,req,id):
        a=Question.objects.filter(id=id).values()
        print(a)
        return JsonResponse({'code': 200,'data':list(a)})
